package com.chap7;

import java.util.concurrent.locks.LockSupport;

/**
 * Created by IntelliJ IDEA.
 * User: bj910
 * Date:2023-03-21
 * Time: 5:07
 * To change this template use File | Settings | File Templates.
 */
public class ParkAndUnparkDemo {
    public static void main(String[] args) {
        ParkAndUnparkThread myThread = new ParkAndUnparkThread(Thread.currentThread());
        myThread.start();
        System.out.println("before park");
        //获取许可，获取不到就会阻塞
        LockSupport.park();
        System.out.println("after park");
    }
}

class ParkAndUnparkThread extends Thread{
    private Object object;

    public ParkAndUnparkThread(Object object) {
        this.object = object;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("before unpark");
        //3秒之后 发放许可
        LockSupport.unpark((Thread) object);
        System.out.println("after unpark");
    }
}
